package com.example.driptip.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.driptip.model.Checklist;

import java.util.ArrayList;
import java.util.List;

public class ChecklistManager {
    private static final String PREF_NAME = "checklist_prefs";
    private static final String KEY_ITEMS = "checklist_items";
    private final SharedPreferences preferences;

    public ChecklistManager(Context context) {
        preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }

    public void saveChecklist(Checklist checklist) {
        SharedPreferences.Editor editor = preferences.edit();
        List<String> items = checklist.getAllItems();
        editor.putString(KEY_ITEMS, String.join("|", items));
        editor.apply();
    }

    public Checklist loadChecklist() {
        Checklist checklist = new Checklist();
        String savedItems = preferences.getString(KEY_ITEMS, "");
        if (!savedItems.isEmpty()) {
            String[] items = savedItems.split("\\|");
            List<String> itemList = new ArrayList<>();
            for (String item : items) {
                itemList.add(item);
            }
            checklist.setAllItems(itemList);
        }
        return checklist;
    }
} 